package com.kgc.web;

import com.kgc.dao.UserDao;
import com.kgc.entity.User;
import com.kgc.utils.CodecUtils;
import com.kgc.vo.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UserLoginServlet", value = "/UserLoginServlet")
public class UserLoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求标识
        String opt = request.getParameter("opt");
        System.out.println(opt);
        PrintWriter writer = response.getWriter();
        HttpSession session = request.getSession();
        UserDao userDao = new UserDao();
        System.out.println(opt);
        if("captcha".equals(opt)) {
            //验证验证码
            //1.获取用户输入验证码
            String captcha = request.getParameter("captcha");
            //2.获取Session验证码
            String code = (String) session.getAttribute("code");
            if(captcha.equalsIgnoreCase(code)) {
                writer.write("ok");
            }else {
                writer.write("error");
            }
        }else if("login".equals(opt)) {//登录功能
            System.out.println("登录");
            String captcha = request.getParameter("captcha");
            // 1.获取Session验证码
            String code = (String) session.getAttribute("code");
            if(captcha.equalsIgnoreCase(code)){
                //2.获取用户名和密码
                String phone = request.getParameter("phone");
                String password = request.getParameter("password");
                System.out.println(phone);
                User user = userDao.selectLogin(phone);
                System.out.println("user = " + user);
                if(user != null){
                    String salt = user.getSalt();
                    if(CodecUtils.match(user.getPassword(),password, salt)){
                        session.setAttribute("user", user);
                        writer.write(R.success("登录成功!").toJSON());
                    }else {
                        writer.write(R.error("登陆失败,密码有误!").toJSON());
                    }
                }else{
                    writer.write(R.error("登录失败,用户名不存在!").toJSON());
                }
            }
        }else if("register".equals(opt)){
            String username = request.getParameter("username");
            String email = request.getParameter("email");
            String phone = request.getParameter("phone");
            int sex = Integer.parseInt(request.getParameter("sex"));
            String salt = CodecUtils.generateSalt();
            String pwd = "123456";
            String password = CodecUtils.md5Hex(pwd,salt);
            int rs = userDao.addUser(username, password, phone, email, sex, salt);
            if( rs > 0 ){
                writer.write(R.success("注册成功!").toJSON());
            }else{
                writer.write(R.error("注册失败!").toJSON());
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
